<template>
	<view class="wrap">
		<view class="top bt">
			<view class="item">
				<view class="left">姓名：</view>
				<input v-model="name" type="text" placeholder-class="line" placeholder="请填写姓名" />
			</view>
			<view class="item" @click="showKeyboard">
				<view class="left">手机号码：</view>
				<input type="number" v-model="mobile" placeholder-class="line" placeholder="请填写手机号" disabled="true"/>
			</view>
			<view class="item flex" @click="showMap">
				<view class="flex_l u-flex-1">
					<view class="left">所在地区：</view>
					<input class="u-flex-1" v-model="address" placeholder-class="line" type="text" disabled 
						placeholder="省市区县、乡镇等" />
				</view>
				<u-icon name="arrow-right" class="ml-10 gray-2"></u-icon>
			</view>
			<view class="item">
				<view class="left">门牌号：</view>
				<input v-model="corridor" type="text" placeholder-class="line" placeholder="街道、门牌号等" />
			</view>
			<view class="item">
				<view class="left">详细地址：</view>
				<input v-model="detail" type="text" placeholder-class="line" placeholder="详细地址" />
			</view>
			<view class="item">
				<view class="left">类型：</view>
				<uni-data-checkbox mode="tag" v-model="type" :localdata="typeArray"></uni-data-checkbox>
			</view>
		</view>

		<view class="bottom">
			<view class="default plr">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips yellow">提醒：每次下单会默认推荐该地址</view>
				</view>
				<view class="right">
					<u-switch v-model="is_default" active-color="red"></u-switch>
				</view>
			</view>
		</view>
		<u-keyboard mode="number" tips="手机号码" @change="valChange" @backspace="backspace" safe-area-inset-bottom="true"
			:dot-enabled="false" z-index="9999" v-model="show"></u-keyboard>
		<view class="pd-30 fixed-bottom" v-if="id">
			<u-button :disabled="!name||!mobile||!address||!corridor" type="warning" @click="edit()">确认修改</u-button>
		</view>
		<view class="pd-30 fixed-bottom" v-else>
			<u-button :disabled="!name||!mobile||!address||!corridor" type="warning" @click="add()">确认添加</u-button>
		</view>
	</view>
</template>

<script>
    import Auth from '@/libs/permission/index.js';
	export default {
		data() {
			return {
				show: false,
				id: null,
				name: '',
				mobile: '',
				address: '',
				corridor: '',
				detail: '',
				lng: 0,
				lat: 0,
				type: 1,
				is_default: false,
				typeArray: [
					{value: 1, text: '住宅'},
					{value: 2, text: '商铺'},
					{value: 3, text: '写字楼'},
					{value: 4, text: '工厂'}
				]
			};
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
				uni.setNavigationBarTitle({
					title: '修改地址'
				})
				this.init()
			}
			// if (option.lat) {
			// 	this.lat = option.lat;
			// }
			// if (option.lng) {
			// 	this.lng = option.lng;
			// }
		},
		methods: {
			showKeyboard() {
				this.show = true;
			},
			valChange(val) {
				this.mobile += val;
			},
			backspace(field) {
				if (this.mobile.length) {
					this.mobile = this.mobile.substr(0, this.mobile.length - 1);
				}
			},
			init() {
				if (this.id) {
					this.$http('/api/address/detail', {
						id: this.id
					}).then(data => {
						this.name = data.name;
						this.mobile = data.mobile;
						this.lng = data.lng;
						this.lat = data.lat;
						this.type = data.type;
						this.address = data.address;
						this.corridor = data.corridor;
						this.detail = data.detail;
						this.is_default = data.is_default;
					});
				}
			},
            async showMap() {
				let _this = this
                let authState = await new Auth('userLocation').check();
                authState &&
                uni.chooseLocation({
                    longitude: _this.lng ? _this.lng : 106.64188,
                    latitude: _this.lat ? _this.lat : 30.47392,
                    success: res => {
                        _this.address = res.address;
                        _this.corridor = res.name;
                        _this.lng = res.longitude;
                        _this.lat = res.latitude;
                    },
                    fail: err => {
						uni.showToast({
							title: '权限被禁止',
							icon: 'error',
							duration: 4000,
						})
                        console.log(err);
                    }
                });
			},
			add() {
				this.$http('/api/address/save', {
					name: this.name,
					mobile: this.mobile,
					lng: this.lng,
					lat: this.lat,
					type: this.type,
					address: this.address,
					corridor: this.corridor,
					detail: this.detail,
					is_default: this.is_default
				}, "POST").then(data => {
					uni.showToast({
						title: '添加成功'
					})
					setTimeout(() => {
						uni.navigateBack({})
					}, 1000)
				})
			},
			edit() {
				this.$http('/api/address/save', {
					id: this.id,
					name: this.name,
					mobile: this.mobile,
					lng: this.lng,
					lat: this.lat,
					type: this.type,
					address: this.address,
					corridor: this.corridor,
					detail: this.detail,
					is_default: this.is_default
				}, "POST").then(data => {
					uni.showToast({
						title: '修改成功'
					})
					setTimeout(() => {
						uni.navigateBack({})
					}, 1000)
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/.is-tag-text {
		font-size: 12px;
	}
	
	/deep/.is-tag {
		padding: 3px 5px;
		height: 20px;
	}
	
	/deep/.is-tag-checked {
		border-color: #F66E23!important;
		background-color: #F66E23!important;
	}
	
	/deep/ .line {
		color: $u-light-color;
		font-size: 28rpx;
	}

	input {
		font-size: 28rpx;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;

			padding: 0 36rpx;

			.item {
				display: flex;
				font-size: 32rpx;
				line-height: 110rpx;
				align-items: center;
				border-bottom: solid 1rpx $bg;

				.left {
					width: 170rpx;
					text-align: right;
					margin-right: 30rpx;
				}

				input {
					text-align: left;
				}
			}

			.address {
				padding: 20rpx 0;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			margin-top: 20rpx;
			padding: 40rpx;
			padding-right: 0;
			background-color: #ffffff;
			font-size: 28rpx;

			.tag {
				display: flex;

				.left {
					width: 165rpx;
				}

				.right {
					display: flex;
					flex-wrap: wrap;

					.tags {
						width: 140rpx;
						padding: 16rpx 8rpx;
						border: solid 2rpx $u-border-color;
						text-align: center;
						border-radius: 50rpx;
						margin: 0 10rpx 20rpx;
						display: flex;
						font-size: 28rpx;
						align-items: center;
						justify-content: center;
						color: $u-content-color;
						line-height: 1;
					}

					.plus {
						//padding: 10rpx 0;
					}
				}
			}

			.default {
				width: 96%;
				margin-top: 30rpx;
				display: flex;
				justify-content: space-between;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
				}

				.right {}
			}
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 90%;
		line-height: 96rpx;
		position: absolute;
		bottom: 50rpx;
		left: 5%;
		background-color: $yellow;
		border-radius: 10rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>
